﻿using System;
using Handler.Business;
using System.Web.SessionState;
using Dln.Common.Helper;
using Dln.Model;

namespace Dln.Handler
{
    class EventHandler : IHandler, IRequiresSessionState
    {
        /// <summary>
        /// 请求的xml
        /// </summary>
        private string RequestXml { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="requestXml"></param>
        public EventHandler(string requestXml)
        {
            this.RequestXml = requestXml;
        }

        /// <summary>
        /// 处理请求
        /// </summary>
        /// <returns></returns>
        public string HandleRequest()
        {
            Log("HandleRequest:" + RequestXml);
            EventMessage clientEventMessage = EventMessage.LoadFromXml(RequestXml);
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.FromUserName = clientEventMessage.ToUserName;
            responseMessage.ToUserName = clientEventMessage.FromUserName;
            responseMessage.ResponseType = ResponseType.Text;
            if (clientEventMessage != null)
            {
                switch (clientEventMessage.Event.ToLower())
                {
                    case "subscribe":
                        responseMessage.Content = SubscribeEventHandler(clientEventMessage.FromUserName, clientEventMessage.EventKey);
                        break;
                    case "click":
                        responseMessage.Content = ClickEventHandler(clientEventMessage);
                        break;
                    case "scancode_waitmsg":
                        responseMessage.Content = Scancode_WaitmsgEventHandler(clientEventMessage);
                        break;
                    case "scan":
                        responseMessage.Content = ScanEventHandler(clientEventMessage);
                        break;
                }
            }

            return responseMessage.ResponseTest;
        }
        /// <summary>
        /// 扫描二维码
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        private string ScanEventHandler(EventMessage em)
        {
            string openId = em.FromUserName;
            string evenKey = em.EventKey.ToUpper();
            return "";
        }
        /// <summary>
        /// 关注
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        private string SubscribeEventHandler(string openId, string key)
        {
            try
            {
                if (!WeChatUsersBusiness.IsExistOpenId(openId))
                {
                    WeChatUsersBusiness.Insert(openId);
                }

                if (String.IsNullOrEmpty(key))
                {
                    return "沭阳县妇幼保健院欢迎您！";
                }
                else
                {
                    return "沭阳县妇幼保健院欢迎您";
                }
            }
            catch (Exception ex)
            {
                return "数据插入失败，请联系信息科";
            }
        }
        /// <summary>
        /// 处理点击事件
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        private string ClickEventHandler(EventMessage em)
        {
            return null;
        }

        /// <summary>
        /// 扫码推事件且弹出“消息接收中”提示框
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        private string Scancode_WaitmsgEventHandler(EventMessage em)
        {
            string openId = em.FromUserName;
            string evenKey = em.EventKey.ToUpper();
            switch (em.EventKey.ToUpper())
            {
                case "RSELFMENU_0_0":
                    return string.Format("您的条形码为{0}", em.ScanCodeInfo.ScanResult);

            }
            return "";
        }

        //记录日志
        private void Log(string content)
        {
            try
            {

            }
            catch (Exception ex)
            {

            }
        }




    }
}